<template>
	<view class="policy-config-form-layout">
		<view class="policy-config-form">
			<view class="policy-config-form-item" v-for="(item,index) in formParams.rateList" :key="index">
				<text>{{item.payTypeName}}</text>
				<xh-input placeholder="请输入" inputTextAlign="center" :isLine="false" keyboardType="digit" height="0" :isSelect="!item.isCanEdit" :showRightIcon="false" v-if="formParams.isOpenEdit" v-model.trim="item.inputValue"></xh-input>
				<text class="range-text" v-if="formParams.isOpenEdit">{{getResultValue(item).rangeValue}}</text>
				<text class="value-text" v-else>{{getResultValue(item).value}}</text>
				<view class="line policy-config-form-item-line"></view>
			</view>
			<xh-empty minHeight="50vh" :show="rateInfo.rateInfoList.length == 0"></xh-empty>
		</view>
		<view class="policy-config-form-item policy-config-form-item-md" v-if="singleRateModel">
			<text>秒到提现费</text>
			<xh-input placeholder="请输入" inputTextAlign="center" :isLine="false" keyboardType="digit" v-if="formParams.isOpenEdit" v-model="singleRateModel.inputValue"></xh-input>
			<text class="range-text" v-if="formParams.isOpenEdit">{{singleRateModel.singleT0CostMin}}~{{singleRateModel.singleT0CostMax}}元/笔</text>
			<text class="value-text" v-else>{{singleRateModel.value}}元/笔</text>
		</view>
		<view class="devcie-rate-operate" v-if="formParams.isCanEdit">
			<view class="devcie-rate-operate-btn-box" v-if="formParams.isOpenEdit">
				<view class="submit-btn submit-btn1" @tap="save">确认保存</view>
				<view class="cancel-btn" @tap="cancel">取消</view>
			</view>
			<view class="devcie-rate-operate-btn-edit" v-if="!formParams.isOpenEdit" @tap="editRate">
				<text>修改结算成本</text>
			</view>
		</view>
	</view>

</template>

<script setup>
	import { onMounted, ref } from 'vue'
	import { bindAgentPolicy, getAgentPolicyRate, updateAgentPolicyRate } from '@/common/api/api.js'

	const props = defineProps({
		agentId: {
			type: String,
			default: ''
		},
		produceId: {
			type: Number,
			default: 0
		},
		policyId: {
			type: Number,
			default: 0
		}
	})

	const rateInfo = ref({
		rateInfoList: []
	})
	const formParams = ref({
		//是否支持修改
		isCanEdit: false,
		//是否开启编辑
		isOpenEdit: false,
		//0 未配置 1 已配置
		rateStatus: '',
		// 机具费率列表
		rateList: []
	})
	// 秒到提现费
	const singleRateModel = ref(null)

	// 显示不可已经设置的费率和范围显示数据
	const getResultValue = (item) => {
		if (item.payTypeName == '借记卡封顶') {
			item.isCanEdit = true
			return {
				value: `${item.value}元`,
				rangeValue: `${item.debitCardTopMin}~${item.debitCardTopMax}元`
			}
		}else if (item.payTypeName == 'creditpay') {
			item.isCanEdit = true
			return {
				value: `${item.value}%`,
				rangeValue: `${item.creditCardRateMin}%~${item.creditCardRateMax}%`
			}
		} else {
			item.isCanEdit = true
			return {
				value: `${item.value}%`,
				rangeValue: `${item.debitCardRateMin}%~${item.debitCardRateMax}%`
			}
		}
	}
	// 修改费率
	const editRate = () => {
		formParams.value.isOpenEdit = true
	}
	//取消
	const cancel = () => {
		if (formParams.value.rateStatus == '0') {
			uni.navigateBack()
		} else {
			formParams.value.isOpenEdit = false
			formParams.value.rateList.forEach(item => {
				item.inputValue = item.value
			})
			singleRateModel.value.inputValue = singleRateModel.value.value
		}
	}
	// 保存
	const save = () => {
		if (formParams.value.rateList.length == 0) {
			uni.$utils.showToast('保存失败，暂无费率信息')
			return
		}
		for (let i = 0; i < formParams.value.rateList.length; i++) {
			const obj = formParams.value.rateList[i]
			if (uni.$uv.trim(obj.inputValue) == '') {
				uni.$utils.showToast(`请输入${obj.payTypeName}`)
				return
			} 
			else {
				const value1 = parseFloat(obj.inputValue) || 0
				if (obj.payTypeCode == '0') {
					const value2 = parseFloat(obj.debitCardTopMax) || 0
					const value3 = parseFloat(obj.debitCardTopMin) || 0
					if (value1 > value2 || value1 < value3) {
						uni.$utils.showToast(`${obj.payTypeName}超出区间范围`)
						return;
					}
				} else {
					const value2 = parseFloat(obj.creditCardRateMax) || 0
					const value3 = parseFloat(obj.creditCardRateMin) || 0
					if (value1 > value2 || value1 < value3) {
						uni.$utils.showToast(`${obj.payTypeName}超出区间范围`)
						return;
					}
				}
			}
		}
		if (uni.$uv.trim(singleRateModel.value.inputValue) == '') {
			uni.$utils.showToast(`请输入${singleRateModel.value.payTypeName}`)
			return
		}
		const value1 = parseFloat(singleRateModel.value.inputValue) || 0
		const value2 = parseFloat(singleRateModel.value.singleT0CostMax) || 0
		const value3 = parseFloat(singleRateModel.value.singleT0CostMin) || 0
		if (!singleRateModel && (value1 > value2 || value1 < value3)) {
			uni.$utils.showToast(`${singleRateModel.value.payTypeName}超出区间范围`)
			return;
		}
		if(formParams.value.rateStatus == '1'){
			updatePolicyRate()
		}
		else{
			bindPolicy()
		}
	}
	//绑定政策
	const bindPolicy = async ()=>{
		let params = {}
		if (props.agentId) {
			params.agentId = props.agentId
		}
		if (props.policyId) {
			params.policyId = props.policyId
		}
		if (props.produceId) {
			params.produceId = props.produceId
		}
		await bindAgentPolicy(params)
		updatePolicyRate()
	}
	// 更新政策费率
	const updatePolicyRate = async ()=>{
		let datas = []
		let topModel = null
		formParams.value.rateList.forEach(item=>{
			if(item.payTypeCode == '0') {
				topModel = item
			}
			else{
				datas.push(item)
			}
		})
		let rateInfoList = []
		datas.forEach(obj=>{
			let dict = {}
			dict.payTypeId = obj.payTypeId
			if(!uni.$uv.test.empty(formParams.value.rateStatus)){
				dict.rateStatus = formParams.value.rateStatus
			}
			if(!uni.$uv.test.empty(obj.id)){
				dict.id = obj.id
			}
			if(obj.payTypeCode == 'creditpay') {
				if(singleRateModel.value){
					dict.singleT0Cost = singleRateModel.value.inputValue
				}
				else{
					dict.singleT0Cost = '0'
				}
				dict.creditCardRate = obj.inputValue
				dict.debitCardRate = obj.inputValue
				dict.debitCardTop = '0'
				dict.creditCardTop = '0'
			}
			else if(obj.payTypeCode == 'debitpay') {
				if(topModel){
					dict.debitCardTop = topModel.inputValue
				}
				else{
					dict.debitCardTop = '0'
				}
				dict.debitCardRate = obj.inputValue
				dict.creditCardRate = obj.inputValue
				dict.singleT0Cost = '0'
				dict.creditCardTop = '0'
			}
			else{
				dict.debitCardRate = obj.inputValue
				dict.creditCardRate = obj.inputValue
				dict.debitCardTop = '0'
				dict.creditCardTop = '0'
				dict.singleT0Cost = '0'
			}
			rateInfoList.push(dict)
		})
		let params = {}
		params.rateInfoList = rateInfoList
		if (props.agentId) {
			params.agentId = props.agentId
		}
		if (props.policyId) {
			params.policyId = props.policyId
		}
		if (props.produceId) {
			params.produceId = props.produceId
		}
		if(formParams.value.rateStatus == '1'){
			params.id = '1'
		}
		await updateAgentPolicyRate(params)
		formParams.value.isOpenEdit = false
		uni.$utils.showToast('保存成功',1000,()=>{
			getAgentPolicyRateData()
		})
	}
	// 获取代理商政策费率
	const getAgentPolicyRateData = async () => {
		let params = {}
		if (props.agentId) {
			params.agentId = props.agentId
		}
		if (props.policyId) {
			params.policyId = props.policyId
		}
		if (props.produceId) {
			params.produceId = props.produceId
		}
		const res = await getAgentPolicyRate(params)
		const data = res.result
		rateInfo.value = data
		formParams.value.rateStatus = data.rateStatus || '0'
		const list = rateInfo.value.rateInfoList
		formParams.value.rateList = []
		list.forEach(obj => {
			if (obj.payTypeCode == 'creditpay') {
				obj.value = obj.creditCardRate
			} else {
				obj.value = obj.debitCardRate
			}
			if (formParams.value.rateStatus == '1') {
				obj.inputValue = obj.value
			}
			obj.isCanEdit = true
			formParams.value.rateList.push(obj)
			if (obj.payTypeCode == 'debitpay') {
				let model = {}
				model.payTypeName = '借记卡封顶'
				model.payTypeCode = '0'
				model.debitCardTop = obj.debitCardTop
				model.debitCardTopMin = obj.debitCardTopMin
				model.debitCardTopMax = obj.debitCardTopMax
				model.value = obj.debitCardTop
				if (formParams.value.rateStatus == '1') {
					model.inputValue = model.value;
				}
				formParams.value.rateList.push(model)
			}
			if (obj.payTypeCode == 'creditpay') {
				let model = {}
				model.payTypeName = '秒到提现费'
				model.payTypeCode = '1'
				model.singleT0CostMin = obj.singleT0CostMin
				model.singleT0CostMax = obj.singleT0CostMax
				model.singleT0Cost = obj.singleT0Cost
				model.value = obj.singleT0Cost
				if (formParams.value.rateStatus == '1') {
					model.inputValue = model.value;
				}
				singleRateModel.value = model
			}
		})
		formParams.value.isCanEdit = false
		if (props.agentId) {
			formParams.value.isCanEdit = true
			if (formParams.value.rateStatus == '0') {
				formParams.value.isOpenEdit = true
			} else {
				formParams.value.isOpenEdit = false
			}
		}
	}

	onMounted(() => {
		console.log('组件挂载')
		getAgentPolicyRateData()
	})
</script>

<style lang="scss" scoped>
	.policy-config-form-layout {
		padding-bottom: 100rpx;

		.policy-config-form {
			background-color: #fff;
			border-radius: 20rpx;
		}

		.policy-config-form-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 30rpx;
			position: relative;
			font-size: 26rpx;
			height: 100rpx;

			.policy-config-form-item-line {
				left: 30rpx;
				right: 30rpx;
			}

			text:nth-of-type(1) {
				flex-shrink: 0;
			}

			.range-text {
				color: $xh-red-color;
				font-size: 26rpx;
				flex-shrink: 0;
			}

			.value-text {
				color: #8E8D92;
				font-size: 26rpx;
				flex-shrink: 0;
			}
		}

		.policy-config-form-item-md {
			margin-top: 30rpx;
			border-radius: 20rpx;
			background-color: #fff;
		}

		.submit-btn-1 {
			margin: 50rpx 0 0;
		}

		.cancel-btn {
			margin-top: 30rpx;
			color: $xh-color-3;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 45rpx;
			font-size: 30rpx;
			text-align: center;
			border: 1px solid #C3C3C3;
		}

		.devcie-rate-operate {
			padding-top: 50rpx;
			.submit-btn1 {
				margin-bottom: 30rpx;
			}
			.devcie-rate-operate-btn-edit {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 30rpx;
				color: $xh-theme-color;
			}
		}
	}
</style>